﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB;
using YJKRevitBase.WinForm;
using YJKOpeningCommon.Public;

namespace YJKOpeningCreate.ConfigForm
{
	public partial class OpeningRemoveForm : ConfigDataBaseForm
	{
		public OpeningRemoveForm(Autodesk.Revit.DB.Document doc, WindowHandle _hWndRevit, List<OpeningData> listOpeningData)
		{
			this.InitializeComponent();
			base.SetFormLocation(this.strXmlPath);
			this.m_ListOpeningData = listOpeningData;
			this.m_doc = doc;
			this.m_HWndRevit = _hWndRevit;
			this.dgDelOpening.RowHeadersVisible = false;
			this.dgDelOpening.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
			this.SetDataGridViewWidth();
			this.UpdateOpeningDataGridShow();
		}

		private void UpdateOpeningDataGridShow()
		{
			this.dgDelOpening.AllowUserToAddRows = true;
			this.dgDelOpening.Rows.Clear();
			this.m_ListOpeningData.Sort();
			foreach (OpeningData openingData in this.m_ListOpeningData)
			{
				this.dgDelOpening.Rows.Add(new object[]
				{
					true,
					this.dgDelOpening.RowCount,
					openingData.m_strSpeciality,
					openingData.m_strUserName,
					openingData.m_strCreateData
				});
				openingData.m_doc = this.m_doc;
				this.dgDelOpening.Rows[this.dgDelOpening.RowCount - 2].Tag = openingData;
			}
			this.dgDelOpening.AllowUserToAddRows = false;
			if (this.m_ListOpeningData.Count > 0)
			{
				this.btn_DelOpening.Enabled = true;
				return;
			}
			this.btn_DelOpening.Enabled = false;
		}

		private void SetDataGridViewWidth()
		{
			double num = (double)(this.dgDelOpening.Width / 5);
			if (this.dgDelOpening.ColumnCount > 4)
			{
				this.dgDelOpening.Columns[0].Width = Convert.ToInt32(num * 0.3);
				this.dgDelOpening.Columns[1].Width = Convert.ToInt32(num * 0.3);
				this.dgDelOpening.Columns[2].Width = Convert.ToInt32(num * 1.2);
				this.dgDelOpening.Columns[3].Width = Convert.ToInt32(num * 1.2);
				this.dgDelOpening.Columns[4].Width = Convert.ToInt32(num * 2.0);
			}
		}

		private void btn_SelNone_Click(object sender, EventArgs e)
		{
			foreach (object obj in ((IEnumerable)this.dgDelOpening.Rows))
			{
				((DataGridViewRow)obj).Cells[0].Value = false;
			}
		}

		private void btn_SelAll_Click(object sender, EventArgs e)
		{
			foreach (object obj in ((IEnumerable)this.dgDelOpening.Rows))
			{
				((DataGridViewRow)obj).Cells[0].Value = true;
			}
		}

		private void btn_DelOpening_Click(object sender, EventArgs e)
		{
			List<OpeningData> list = new List<OpeningData>();
			using (TransactionGroup transactionGroup = new TransactionGroup(this.m_doc))
			{
				transactionGroup.Start("TransGroupDelModelGroup");
				try
				{
					bool flag = true;
					bool flag2 = true;
					foreach (object obj in ((IEnumerable)this.dgDelOpening.Rows))
					{
						DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
						OpeningData openingData = dataGridViewRow.Tag as OpeningData;
						if (openingData != null)
						{
							if (dataGridViewRow.Cells[0].Value == null || !(bool)dataGridViewRow.Cells[0].Value)
							{
								list.Add(openingData);
							}
							else
							{
								flag2 = false;
								if (!openingData.RemoveOpenings(this.m_HWndRevit))
								{
									flag = false;
									break;
								}
							}
						}
					}
					if (flag)
					{
						transactionGroup.Assimilate();
						if (flag2)
						{
							YJKMessageBox.Show("没有选择任何数据");
						}
						else
						{
							YJKMessageBox.Show("所选洞口成功删除");
						}
						this.m_ListOpeningData = list;
						this.UpdateOpeningDataGridShow();
					}
					else
					{
						transactionGroup.RollBack();
						YJKMessageBox.Show("删除洞口出现错误，已被删除的洞口将被还原，命令结束。");
					}
				}
				catch
				{
					transactionGroup.RollBack();
					YJKMessageBox.Show("删除洞口出现错误，已被删除的洞口将被还原，命令结束。");
				}
			}
		}

		private void btn_Cancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void OpeningRemoveForm_MouseEnter(object sender, EventArgs e)
		{
			new ToolTip
			{
				ShowAlways = true
			}.SetToolTip(this.btn_DelOpening, "对话框只是列出当前活动工作集的洞");
		}

		private List<OpeningData> m_ListOpeningData;

		private Document m_doc;

		private WindowHandle m_HWndRevit;

		private string strXmlPath = Product.UserDataLocation + "\\OpeningRemoveForm.xml";
	}
}
